package com.chianghao.rock.system.service.user;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chianghao.core.exception.HaoException;
import com.chianghao.core.frame.service.BaseService;
import com.chianghao.core.mybatis.plugins.Paging;
import com.chianghao.core.utils.DesUtils;
import com.chianghao.rock.Constant;
import com.chianghao.rock.common.SequenceService;
import com.chianghao.rock.system.dao.route.RouteDao;
import com.chianghao.rock.system.dao.route.RouteMapper;
import com.chianghao.rock.system.dao.user.UserDao;
import com.chianghao.rock.system.dao.user.UserMapper;
import com.chianghao.rock.system.entity.Route;
import com.chianghao.rock.system.entity.User;
import com.chianghao.rock.system.service.organization.OrganizationService;

@Service
public class UserService extends BaseService<UserMapper, User> {

	
	@Autowired
    SequenceService sequenceService;
	
	@Autowired
	RouteDao routeDao;
	
	@Autowired
	RouteMapper routeMapper;
	
	@Autowired
	OrganizationService organizationService;
	
	@Autowired
	UserDao dao;
	
	
	public void doCreateUser(User user,Integer isSupperAdmin) {
		//教研账号唯一性
		
		
		user.setIsSupperAdmin(isSupperAdmin);
		user.setId(sequenceService.getNextId());
		//对密码进行加密
		String password = DesUtils.encrypt(user.getPassword(), Constant.passwordKey);
		user.setPassword(password);
		this.mapper.insert(user);
	}

	
	public void doSetNewPassword(String id, String newPassword) {
		String password = DesUtils.encrypt(newPassword, Constant.passwordKey);
		User user = new User();
		user.setPassword(password);
		this.updateById(user, id);
	}


	public void upatePassword(String oldPassword, String newPassword, Long userId) {
		User user = this.queryOneById(userId);
		String oldPasswordDes  = DesUtils.decrypt(user.getPassword(), Constant.passwordKey);
		if(!oldPasswordDes.equals(oldPassword)) {
			throw new HaoException("error:old.password.error");
		}
		User field = new User();
		field.setPassword(DesUtils.encrypt(newPassword, Constant.passwordKey));
		this.updateById(field, userId);
	}


	/**
	 * 账号密码登录
	 * @param account
	 * @param password
	 * @return
	 */
	public User login(String account,String password) {
		User where  = new User();
		where.setAccount(account);
		User user  = this.mapper.queryOneByEntity(where);
		if(user==null) {
			throw new HaoException("error:account,not.exist");
		}
		String passwordDes  = DesUtils.decrypt(user.getPassword(), Constant.passwordKey);
		if(!password.equals(passwordDes)) {
			throw new HaoException("error:password,error");
		}
		return user;
	}


	/**
	 * 查新用户的route
	 * @param userId
	 * @return
	 */
	public List<Route> queryUserRoute(Long userId) {
		User user  = this.queryOneById(userId);
		if(user.getIsSupperAdmin().intValue()==1) {
			return routeDao.queryAllRoute();
		}else {
			return null;
		}
	}


	public List<User> queryAppSupperAdmin() {
		User where = new User();
		where.setIsSupperAdmin(1);
		return this.mapper.queryListByEntity(where, null);
	}


	public List<User> queryPage(Long organizationId,boolean isShowNoOrgUser,String name, Paging paging) {
		List<Long> allOrganizationIds = null;
		if(organizationId!=null) {
			Set<Long> ids = organizationService.getAllChildId(organizationId);
			if(ids==null) {
				ids = new HashSet<Long>();
			}
			ids.add(organizationId);
			allOrganizationIds = new ArrayList<Long>(ids);
		}
		return this.dao.page(allOrganizationIds,isShowNoOrgUser,name,paging);
	}


	public synchronized void checkHasAccount(String account) {
		int count = this.dao.queryAccount(account);
		if(count>0) {
			throw new HaoException("error:account.exist");
		}
	}


	public void checkHasAccount(String account, Long id) {
		int count = this.dao.queryAccountNoId(account,id);
		if(count>0) {
			throw new HaoException("error:account.exist");
		}
	}




}